package timing.ukulele.portal.api;

import timing.ukulele.portal.service.SystemService;
import timing.ukulele.common.ResponseResult;
import timing.ukulele.common.portal.SystemData;
import timing.ukulele.portal.persistent.System;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/system")
public class SystemApi {
    private final SystemService systemService;

    public SystemApi(SystemService systemService) {
        this.systemService = systemService;
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("根据id获取")
    public ResponseResult<SystemData> getById(@PathVariable("id") Long id) {
        final System system = systemService.getById(id);
        if (system != null) {
            SystemData data = new SystemData();
            BeanUtils.copyProperties(system, data);
            return ResponseResult.success(data);
        }
        return ResponseResult.success();
    }

    @GetMapping("/all")
    @ApiOperation("获取所有业务系统")
    public ResponseResult<List<SystemData>> all() {
        final List<System> list = systemService.list();
        if (!CollectionUtils.isEmpty(list)) {
            List<SystemData> dataList = new ArrayList<>(list.size());
            list.forEach(system -> {
                SystemData data = new SystemData();
                BeanUtils.copyProperties(system, data);
                dataList.add(data);
            });
            return ResponseResult.success(dataList);
        }
        return ResponseResult.success();
    }

    @GetMapping("/getSystemIdByUserId/{userId}")
    public ResponseResult<List<Long>> getSystemIdByUserId(@PathVariable("userId") Long userId){
        return ResponseResult.success(systemService.getSystemIdByUserId(userId));
    }


    @GetMapping("/getLikeSystemName/{systemName}")
    public ResponseResult<List<SystemData>> getLikeSystemName(@PathVariable("systemName") String systemName){
        return ResponseResult.success(systemService.getLikeSystemName(systemName));
    }
}
